package service;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.fragment.app.Fragment;

import com.example.save.R;

public class bing extends Fragment {

    private MyConn myConn;
    private Button Playbutton;
    private Button Pausebutton;
    private Button Nextbutton;


    public bing(){
        super(R.layout.fragment8);
    }

    public class MyConn implements ServiceConnection {

        public MusicService2.MyBinder myBinder=null;

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myBinder=(MusicService2.MyBinder)service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    }

    @Override
    public void onViewCreated(View view, Bundle savedStanceState){
        super.onViewCreated(view,savedStanceState);

        Intent intent=new Intent(getActivity(),MusicService2.class);
        getActivity().startService(intent);

        //bindService
        if(myConn==null){
            myConn=new MyConn();
            intent=new Intent(getActivity(),MusicService2.class);
            getActivity().bindService(intent,myConn,0);
        }

        Playbutton=getView().findViewById(R.id.button8);
        Playbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(myConn.myBinder==null){
                    return ;
                }
                myConn.myBinder.play();
            }
        });

        Pausebutton=getView().findViewById(R.id.button10);
        Pausebutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(myConn.myBinder==null){
                    return ;
                }
                myConn.myBinder.pause();
            }
        });

        Nextbutton=getView().findViewById(R.id.button9);
        Nextbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(myConn.myBinder==null){
                    return ;
                }
                myConn.myBinder.next();
            }
        });
    }
}
